package demo4.mapper;

import demo4.pojo.User;
import demo4.utill.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private IDBUtil dbUtil;
    @Override
    public int insert(User user){
        try(Connection connection = dbUtil.getConnection()) {
            String sql = "insert into user(username,password,nickname,age) " +
                    "values(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());

            return ps.executeUpdate();


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    @Override
    public User selectByUserName(String userName){
        try(Connection connection = dbUtil.getConnection()) {
            String sql = "select * from user where username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,userName);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
            return null;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public List<User> selectAll(){
        List<User> list = new ArrayList<>();
        try(Connection connection = dbUtil.getConnection()) {
            String sql = "select id,username,password,nickname,age from user";
            Statement ps = connection.createStatement();
            ResultSet rs = ps.executeQuery(sql);
            while(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                list.add(user);
            }
            return list;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int userUpdate(User user){
        try(Connection connection = dbUtil.getConnection()) {
            String sql = "update user set password=?,nickname=?,age=? where username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getPassword());
            ps.setString(2,user.getNickname());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getUsername());
            return ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
